package ph.gov.marikina.mcsmtms.tests;

import static org.junit.Assert.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import ph.gov.marikina.mcsmtms.constants.GlobalConstants.Direction;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.ReportSource;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.Roads;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.SituationTypes;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.Status;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.TrafficConditionTypes;
import ph.gov.marikina.mcsmtms.dao.SituationReportDAO;
import ph.gov.marikina.mcsmtms.domain.Road;
import ph.gov.marikina.mcsmtms.domain.SituationReport;
import ph.gov.marikina.mcsmtms.domain.SituationSegment;
import ph.gov.marikina.mcsmtms.utils.Utils;

public class SituationReportDAOImplTest {

	Logger log = Logger.getLogger(SituationReportDAOImplTest.class);

	/** The context. */
	private static ApplicationContext springContext = new ClassPathXmlApplicationContext(
			"spring/test-spring-config.xml");
	
	/** The application context. */
//	private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
//			"applicationContext.xml");
	
	@Before
	public void setUp() throws Exception {
	}

	@Test
	public void testAddSituationReport() {
		//fail("Not yet implemented");
		SituationReportDAO situationReportDAO = (SituationReportDAO) springContext
				.getBean("situationReportDAO");
		SituationReport situationReport = new SituationReport();
		
		situationReport.setRoad(Roads.BAYAN_BAYANAN.getCode());
		situationReport.setDirection(Direction.A_TO_B.getCode());
		situationReport.setLastUpdateDateTime(new Date());
		situationReport.setReporter(18);
		situationReport.setReportSource(ReportSource.WEB.getCode());
		situationReport.setStatus(Status.NEW.getCode());
		
		SituationSegment situationSegment = new SituationSegment();
		situationSegment.setSituation(SituationTypes.ACCIDENT.getCode());
		situationSegment.setAccessibleYn(true);
		situationSegment.setTrafficCondition(TrafficConditionTypes.HEAVY.getCode());
		situationSegment.setDescription("Test");
		situationSegment.setStartLat(new Float(0.0));
		situationSegment.setStartLng(new Float(0.0));
		situationSegment.setEndLat(new Float(0.0));
		situationSegment.setEndLng(new Float(0.0));
		situationSegment.setSituationReport(situationReport);
		
		List<SituationSegment> situationSegmentList = new ArrayList<SituationSegment>();
		situationSegmentList.add(situationSegment);
		situationReport.setSituationSegmentList(situationSegmentList);
		
		situationReportDAO.addSituationReport(situationReport);
		
		Assert.assertTrue(true);
		//Assert
	}
	
	@Ignore
	public void testGetHazardMapSituationReports() {
		//fail("Not yet implemented");
		SituationReportDAO situationReportDAO = (SituationReportDAO) springContext
				.getBean("situationReportDAO");
		List<SituationReport> situationReportList = situationReportDAO.getHazardMapSituationReports();
		
		for(SituationReport situationReport : situationReportList) {
			System.out.println("SituationReportID: " + situationReport.getId());
			for(SituationSegment segment : situationReport.getSituationSegmentList()) {
				System.out.println("SituationSegmentID: " + segment.getId());
			}
		}
		
		assertTrue(situationReportList.size() > 0);
	}
	
	@Test
	public void testGetReportsForDateRange() {
		//fail("Not yet implemented");
		SituationReportDAO situationReportDAO = (SituationReportDAO) springContext
				.getBean("situationReportDAO");
		
		Date startDate = Utils.parseDate("04/10/2013");
		Date endDate = Utils.parseDate("04/10/2013");
		
		List<SituationReport> situationReportList = situationReportDAO.getReportsForDateRange(startDate, endDate, Status.VALIDATED.getCode());
		
		for(SituationReport situationReport : situationReportList) {
			System.out.println("SituationReportID: " + situationReport.getId());
			for(SituationSegment segment : situationReport.getSituationSegmentList()) {
				System.out.println("SituationSegmentID: " + segment.getId());
			}
		}
		
		assertTrue(situationReportList.size() > 0);
	}
	
	@Test
	public void testSituationCountForDateRange() {
		//fail("Not yet implemented");
		SituationReportDAO situationReportDAO = (SituationReportDAO) springContext
				.getBean("situationReportDAO");
		
		Date startDate = Utils.parseDate("01/01/2013");
		Date endDate = Utils.parseDate("12/31/2013");
		
		List<Object[]> situationCountList = situationReportDAO.getSituationCountsForDateRange(startDate, endDate, Status.VALIDATED.getCode());
		
		for(Object[] situationCountDetails : situationCountList) {
			if(null != situationCountDetails[0] && null != situationCountDetails[1]) {
				String road = situationCountDetails[0].toString();
				String situation = situationCountDetails[1].toString();
				int count = Integer.parseInt(situationCountDetails[2].toString());
			
				System.out.println(road + ": " + situation + ": " + count);
			}
		}
		
		assertTrue(situationCountList.size() > 0);
	}
	
	@Test
	public void testGetFrequentlyFloodedStreets() {
		//fail("Not yet implemented");
		SituationReportDAO situationReportDAO = (SituationReportDAO) springContext
				.getBean("situationReportDAO");
		
		Date startDate = Utils.parseDate("01/01/2013");
		Date endDate = Utils.parseDate("12/31/2013");
		
		List<Object[]> floodedRoadList = situationReportDAO.getFrequentlyFloodedStreets(startDate, endDate, Status.VALIDATED.getCode());
		
		for(Object[] situationCountDetails : floodedRoadList) {
			if(null != situationCountDetails[0] && null != situationCountDetails[1]) {
				String road = situationCountDetails[0].toString();
				Date reportDate = (Date) situationCountDetails[1];
				Date reportLastDate = (Date) situationCountDetails[2];
				
				System.out.println(road + ": " + reportDate + ": " + reportLastDate);
			}
		}
		
		assertTrue(floodedRoadList.size() > 0);
	}
	
	@Test
	public void testMostAccidentProneStreets() {
		//fail("Not yet implemented");
		SituationReportDAO situationReportDAO = (SituationReportDAO) springContext
				.getBean("situationReportDAO");
		
		Date startDate = Utils.parseDate("01/01/2013");
		Date endDate = Utils.parseDate("12/31/2013");
		
		List<Object[]> accidentProneRoadList = situationReportDAO.getMostAccidentProneStreets(startDate, endDate, Status.VALIDATED.getCode());
		
		for(Object[] situationCountDetails : accidentProneRoadList) {
			if(null != situationCountDetails[0] && null != situationCountDetails[1]) {
				String road = situationCountDetails[0].toString();
				Date reportDate = (Date) situationCountDetails[1];
				Date reportLastDate = (Date) situationCountDetails[2];
				
				System.out.println(road + ": " + reportDate + ": " + reportLastDate);
			}
		}
		
		assertTrue(accidentProneRoadList.size() > 0);
	}
	
	@Test
	public void testMostCongestedStreets() {
		//fail("Not yet implemented");
		SituationReportDAO situationReportDAO = (SituationReportDAO) springContext
				.getBean("situationReportDAO");
		
		Date startDate = Utils.parseDate("01/01/2013");
		Date endDate = Utils.parseDate("12/31/2013");
		
		List<Object[]> mostCongestedRoadsList = situationReportDAO.getMostCongestedStreets(startDate, endDate, Status.VALIDATED.getCode());
		
		for(Object[] mostCongestedRoad : mostCongestedRoadsList) {
			if(null != mostCongestedRoad[0] && null != mostCongestedRoad[1]) {
				String road = mostCongestedRoad[0].toString();
				Date reportDate = (Date) mostCongestedRoad[1];
				Date reportLastDate = (Date) mostCongestedRoad[2];
				String reason = mostCongestedRoad[3].toString();
				
				System.out.println(road + ": " + reportDate + ": " + reportLastDate + ": " + reason);
			}
		}
		
		assertTrue(mostCongestedRoadsList.size() > 0);
	}
	
	@Test
	public void testAccidentCountsPerRoad() {
		//fail("Not yet implemented");
		SituationReportDAO situationReportDAO = (SituationReportDAO) springContext
				.getBean("situationReportDAO");
		
		Date startDate = Utils.parseDate("01/01/2013");
		Date endDate = Utils.parseDate("12/31/2013");
		
		List<Object[]> accidentCountList = situationReportDAO.getAccidentCountsPerRoad(startDate, endDate, Status.VALIDATED.getCode());
		
		for(Object[] situationCountDetails : accidentCountList) {
			if(null != situationCountDetails[0] && null != situationCountDetails[1]) {
				String road = situationCountDetails[0].toString();
				int count = Integer.parseInt(situationCountDetails[1].toString());
				
				System.out.println(road + ": " + count );
			}
		}
		
		assertTrue(accidentCountList.size() > 0);
	}
	
	@Test
	public void testGetNotificationsForRoad() {
		String road = "BAYAN_AVE";
		
		SituationReportDAO situationReportDAO = (SituationReportDAO) springContext.getBean("situationReportDAO");
		
		List<SituationReport> notificationList = situationReportDAO.getForValidationNotifications(road);
		assertNotNull(notificationList);
		assertTrue(notificationList.size() > 0);
	}


}
